<?php
class ManagerAction extends CommonAction {
	// 角色信息编辑列表
	public function modify () {
		$search = $_GET['username'] ? $_GET['username'] : '';
		$map['username'] = array('like', '%' . $search . '%');
		$Manager = D('Manager');
		$count = $Manager->where($map)->count();// 查询满足要求的总记录数
		import('ORG.Util.Page');// 导入分页类
		$Page = new Page($count, C('PAGESIZE'));// 实例化分页类 传入总记录数和每页显示的记录数
		$pageshow = $Page->show();// 分页显示输出
		$list = $Manager->getUsersInfo($search, $Page->firstRow, $Page->listRows);

		$this->assign('search', $search);
		$this->assign('list', $list);
		$this->assign('pageshow', $pageshow);
		$this->display();
	}

	// 编辑表单页
	public function modifyDo () {
		if($_POST) {
			$Manager = D('Manager');
			if($Manager->create()) {
				if($Manager->save()) {
					$this->ajaxReturn(1, L('_FORM_SUCCESS_'), 1);
				} else {
					$this->ajaxReturn(0, $Manager->getError(), 0);
				}
			} else {
				$this->ajaxReturn(0, $Manager->getError(), 0);
			}
		} else {
			$id = $_GET['id'];
			$Manager = M('Manager');
			$data = $Manager->where('id='.$id)->select();
			$Role = M('Role');
			$RoleList = $Role->select();

			$this->assign('data', $data[0]);
			$this->assign('RoleList', $RoleList);
			$this->display();
		}
	}

	// 节点设置新建页
	public function add () {
		if($_POST) {
			$data = $_POST;
			$Manager = D('Manager');
			if($Manager->create()) {
				$data['secretkey'] = getRandString(10);
				$data['password'] = md5($data['password'] . $data['secretkey']);
				$data['create_time'] = time();
				
				if($Manager->add($data)) {
					$this->ajaxReturn(1, L('_FORM_SUCCESS_'), 1);
				} else {
					$this->ajaxReturn(0, $Manager->getError(), 0);
				}
			} else {
				$this->ajaxReturn(0, $Manager->getError(), 0);
			}
		} else {
			$Role = M('Role');
			$RoleList = $Role->select();

			$this->assign('RoleList', $RoleList);
			$this->display();
		}
	}

}